<?

/**
* Fetch list of currency
* 
* @param  string    $COracle   
* @param  string    $srcDb  
* @param  array     $lstCurrency 
*
*/
function fetchLstCurrency(&$COracle,
                          &$srcDb,
                          &$lstCurrency){
                            
  $query  = "SELECT CRR_SYMBOL ".
            "FROM ".$srcDb." ".
            "ORDER BY CRR_SYMBOL ASC ";
            
  $COracle->execFetchQuery($query,
                           $nrows,
                           $result);
  
  $lstCurrency = $result["CRR_SYMBOL"];
}   

/**
* Fetch currently selected of currency
* 
* @param  string    $COracle   
* @param  string    $srcDb  
* @param  array     $selected 
*
*/
function fetchSelCurrency(&$COracle,
                          &$srcDb,
                          &$selected){
                            
  $query  = "SELECT CRR_SYMBOL ".
            "FROM ".$srcDb." ";
            
  $COracle->execFetchQuery($query,
                           $nrows,
                           $result);
  
  $selected = $result["CRR_SYMBOL"][0];
}   

/**
* Fetch list of levelD
* 
* @param  string    $COracle   
* @param  string    $srcDb  
* @param  array     $lstLevelD 
*
*/
function fetchLstLevelD(&$COracle,
                        &$srcDb,
                        &$lstLevelD){
  
  $query  = "SELECT LEVELD ".
            "FROM ".$srcDb." ".
            "ORDER BY LEVELD ASC ";
            
  $COracle->execFetchQuery($query,
                           $nrows,
                           $result);
                           
  $lstLevelD = $result["LEVELD"];
}

/**
* Fetch currently selected levelD
*
*/
function fetchSelLevelD(&$COracle,
                        &$srcDb,
                        &$selected){

  $query  = "SELECT LEVELD ".
            "FROM ".$srcDb." ".
            "WHERE  STATUS='"._ACTIVE."' ";
  
  $COracle->execFetchQuery($query,
                           $nrows,
                           $result);
  
  $selected = $result["LEVELD"][0];               
}

/**
* Fetch list of method
* 
* @param  string    $COracle   
* @param  string    $srcDb  
* @param  array     $lstMethod 
*
*/
function fetchLstMethod(&$COracle,
                        &$srcDb,
                        &$lstMethod){
  
  $query  = "SELECT METHOD_NAME ".
            "FROM ".$srcDb." ".
            "ORDER BY METHOD_NAME ASC ";
            
  $COracle->execFetchQuery($query,
                           $nrows,
                           $result);
                           
  $lstMethod = $result["METHOD_NAME"];
}

/**
* Fetch currently selected method
*
* @param  string    $COracle   
* @param  string    $srcDb  
* @param  array     $selected 
*
*/
function fetchSelMethod(&$COracle,
                        &$srcDb,
                        &$selected){

  $query  = "SELECT METHOD_NAME ".
            "FROM ".$srcDb." ".
            "WHERE  STATUS='"._ACTIVE."' ";
  
  $COracle->execFetchQuery($query,
                           $nrows,
                           $result);
  
  $selected = $result["METHOD_NAME"][0];               
}

/**
* fetch id of currency based on CRR_SYMBOL
*
* @param  string    $COracle
* @param  string    $srcDb
* @param  string    $idCurrency
* @param  string    $nmCurrency
*
*/
function fetchIdCurrency(&$COracle,
                         &$srcDb,
                         &$idCurrency,
                         &$nmCurrency){

  $query  = "SELECT CRR_ID ".
            "FROM ".$srcDb." ".
            "WHERE  CRR_SYMBOL='".$nmCurrency."' ";
  
  $COracle->execFetchQuery($query,
                           $nrows,
                           $result);
                           
  $idCurrency = $result["CRR_ID"][0];
}

/**
* fetch the sequence data detail
*
* @param  string $COracle
* @param  string $srcDb
* @param  string $nSeq
* @param  string $seqDetail
*
*/
function fetchSeqDetail( &$COracle,
                         &$srcDb,
                         &$nSeq,
                         &$seqDetail){

  $query  = "SELECT *
             FROM ".$srcDb." ";
             
  $COracle->execFetchQuery($query,
                           $nSeq,
                           $seqDetail); 
} 

/**
* fetch the latest entry of backup sequence
*
* @param  string $COracle
* @param  string $srcDbHdr
* @param  string $srcDbDtl
* @param  string $nSeq
* @param  string $seqDetail
*
*/
function fetchLatestSeqEntry(&$COracle,
                             &$srcDbHdr,
                             &$srcDbDtl,
                             &$nSeq,
                             &$seqDetail){
                             
  /**
  * if we encounter more than one entry 
  * of the latest date than we need to fetch
  * the latest No of current date
  *
  */
  $query  = "SELECT TR_DATE, COUNT(*) AS N_ENTRY
             FROM ".$srcDbHdr." 
             GROUP BY TR_DATE
             ORDER BY TR_DATE DESC ";
             
  $COracle->execFetchQuery($query,
                           $nrows,
                           $result);
  
  if($nrows > 0 ){                       
    
    if($result["N_ENTRY"][0] > 1){
      
      $strDate = date("d-m-Y",
                      strtotime($result["TR_DATE"][0]));
      
      $theDate = " TO_DATE('".$strDate."','dd-mm-YYYY') ";
      
      $query  = "SELECT DATA_ID
                 FROM ".$srcDbHdr."
                 WHERE  TR_DATE=".$theDate." 
                 ORDER BY NO DESC ";
                 
      $COracle->execFetchQuery($query,
                               $nrows,
                               $result);
                               
      $query  = "SELECT * 
                 FROM ".$srcDbDtl." 
                 WHERE  DATA_ID='".$result["DATA_ID"][0]."' ";
                 
      $COracle->execFetchQuery($query,
                               $nSeq,
                               $seqDetail);
    }else{
      
      $query  = "SELECT * 
                 FROM ".$srcDbDtl." 
                 WHERE  DATA_ID='".$result["DATA_ID"][0]."' ";
                 
      $COracle->execFetchQuery($query,
                               $nSeq,
                               $seqDetail);
    }
  }else{
    
    Misc::usrDie("Error: Backup table is empty");
  }
}
?>